/*
 * PlayerManagerPanel.java
 *
 * Created on October 9, 2008, 11:26 AM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.slimserver.musicjukebox.ui.components.TrackPositionSlider;
import com.bff.bjj.utils.Utils;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import org.bff.slimserver.SlimPlayer;
import org.bff.slimserver.events.SleepChangeEvent;
import org.bff.slimserver.events.SleepChangeListener;
import org.bff.slimserver.exception.SlimConnectionException;

/**
 *
 * @author  bfindeisen
 */
public class PlayerManagerDetailPanel
        extends BJJPanel
        implements SleepChangeListener {

    private SlimPlayer player;
    private JukeboxProperties jp;
    private Timer sleepTimer;
    private long sleepTime;
    private SleepThread sleepThread;
    private static final String MSG_NOT_SLEEPING = "Not set to sleep.";

    /** Creates new form PlayerManagerPanel */
    public PlayerManagerDetailPanel(SlimPlayer player) {
        setPlayer(player);
        jp = JukeboxProperties.getInstance();

        initComponents();

        checkSync();

        try {
            volumeSlider.setValue(getPlayer().getVolume());
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        setLabels();

        sleepTimer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                lblSleep.setText("Sleep in " + Utils.formatTime(getSleepTime()));
                setSleepTime(getSleepTime() - 1);
            }
        });

        try {
            setSleepTime(player.getSleepTime());
            if (getSleepTime() > 0) {
                sleepTimer.start();
                setSleepComponentAbility(false);

            } else {
                setSleepComponentAbility(true);
            }
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        jp.getSlimMonitor().addSleepChangeListener(this);
    }

    public void setCurrent(boolean isCurrent) {
        if (isCurrent) {
            btnSet.setEnabled(false);
        } else {
            btnSet.setEnabled(true);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel3 = new javax.swing.JLabel();
        lblIp = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblModel = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblDisplayType = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnSet = new BJJButton();
        lblSignalStrength = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblPort = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        lblMac = new javax.swing.JLabel();
        jPanel2 = new BJJPanel();
        jLabel2 = new javax.swing.JLabel();
        lblPower = new javax.swing.JLabel();
        btnPower = new BJJButton();
        lblPlayerName = new javax.swing.JLabel();
        jPanel1 = new BJJPanel();
        btnVolumeStart2 = new javax.swing.JButton();
        btnVolumeStart2.setText(null);
        btnVolumeStart2.setContentAreaFilled(false);
        volumeSlider = new TrackPositionSlider();
        btnVolumeEnd = new javax.swing.JButton();
        btnVolumeEnd.setText(null);
        btnVolumeEnd.setContentAreaFilled(false);
        jPanel6 = new BJJPanel();
        jPanel3 = new BJJPanel();
        lblColon = new javax.swing.JLabel();
        cbHour = new javax.swing.JComboBox();
        cbMinute = new javax.swing.JComboBox();
        btnSleep = new BJJButton();
        jPanel4 = new BJJPanel();
        lblSleep = new javax.swing.JLabel();
        btnCancel = new BJJButton();
        jPanel5 = new BJJPanel();
        lblSync = new javax.swing.JLabel();
        btnUnsync = new BJJButton();
        btnSync = new BJJButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Player Settings"));
        setLayout(new java.awt.GridBagLayout());

        jLabel3.setText("IP Address:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 10);
        add(jLabel3, gridBagConstraints);

        lblIp.setText("jLabel3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblIp, gridBagConstraints);

        jLabel4.setText("Model:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 10);
        add(jLabel4, gridBagConstraints);

        lblModel.setText("jLabel3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblModel, gridBagConstraints);

        jLabel5.setText("Display Type:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 10);
        add(jLabel5, gridBagConstraints);

        lblDisplayType.setText("jLabel3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblDisplayType, gridBagConstraints);

        jLabel6.setText("Signal Strength:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 10);
        add(jLabel6, gridBagConstraints);

        btnSet.setText("Set as Current");
        btnSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(btnSet, gridBagConstraints);

        lblSignalStrength.setText("jLabel3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblSignalStrength, gridBagConstraints);

        jLabel7.setText("Port:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 10);
        add(jLabel7, gridBagConstraints);

        lblPort.setText("jLabel3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblPort, gridBagConstraints);

        jLabel8.setText("Mac Address:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 10);
        add(jLabel8, gridBagConstraints);

        lblMac.setText("jLabel3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblMac, gridBagConstraints);

        jPanel2.setBackground(new java.awt.Color(172, 172, 172));

        jLabel2.setText("Powered:");
        jPanel2.add(jLabel2);

        lblPower.setText("jLabel3");
        jPanel2.add(lblPower);

        btnPower.setText("Power");
        btnPower.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPowerActionPerformed(evt);
            }
        });
        jPanel2.add(btnPower);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(jPanel2, gridBagConstraints);

        lblPlayerName.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblPlayerName.setText("jLabel3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        add(lblPlayerName, gridBagConstraints);

        btnVolumeStart2.setBackground(java.awt.Color.lightGray);
        btnVolumeStart2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/slimserver/volume_start.png"))); // NOI18N
        btnVolumeStart2.setBorder(null);
        btnVolumeStart2.setFocusPainted(false);
        btnVolumeStart2.setMaximumSize(new java.awt.Dimension(50, 50));
        btnVolumeStart2.setMinimumSize(new java.awt.Dimension(32, 32));
        btnVolumeStart2.setPreferredSize(new java.awt.Dimension(32, 32));
        btnVolumeStart2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVolumeStart2ActionPerformed(evt);
            }
        });
        jPanel1.add(btnVolumeStart2);

        volumeSlider.setBackground(java.awt.Color.lightGray);
        volumeSlider.setForeground(java.awt.Color.blue);
        volumeSlider.setMajorTickSpacing(25);
        volumeSlider.setMinorTickSpacing(5);
        volumeSlider.setPaintTicks(true);
        volumeSlider.setValue(100);
        volumeSlider.setFocusable(false);
        volumeSlider.setMinimumSize(new java.awt.Dimension(150, 25));
        volumeSlider.setOpaque(false);
        volumeSlider.setPreferredSize(new java.awt.Dimension(150, 25));
        volumeSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                volumeSliderStateChanged(evt);
            }
        });
        jPanel1.add(volumeSlider);

        btnVolumeEnd.setBackground(java.awt.Color.lightGray);
        btnVolumeEnd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/slimserver/volume_end.png"))); // NOI18N
        btnVolumeEnd.setBorder(null);
        btnVolumeEnd.setFocusPainted(false);
        btnVolumeEnd.setMaximumSize(new java.awt.Dimension(50, 50));
        btnVolumeEnd.setPreferredSize(new java.awt.Dimension(48, 48));
        btnVolumeEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVolumeEndActionPerformed(evt);
            }
        });
        jPanel1.add(btnVolumeEnd);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(1, 6, 6, 6);
        add(jPanel1, gridBagConstraints);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Sleep Details"));
        jPanel6.setLayout(new java.awt.GridBagLayout());

        jPanel3.setLayout(new java.awt.GridBagLayout());

        lblColon.setText(":");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        jPanel3.add(lblColon, gridBagConstraints);

        cbHour.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        jPanel3.add(cbHour, gridBagConstraints);

        cbMinute.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        jPanel3.add(cbMinute, gridBagConstraints);

        btnSleep.setText("Sleep");
        btnSleep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSleepActionPerformed(evt);
            }
        });
        jPanel3.add(btnSleep, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 6, 6);
        jPanel6.add(jPanel3, gridBagConstraints);

        lblSleep.setText("Sleep in 00:00:00");
        jPanel4.add(lblSleep);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel4.add(btnCancel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        jPanel6.add(jPanel4, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(jPanel6, gridBagConstraints);

        jPanel5.add(lblSync);

        btnUnsync.setText("UnSync");
        btnUnsync.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUnsyncActionPerformed(evt);
            }
        });
        jPanel5.add(btnUnsync);

        btnSync.setText("Sync");
        btnSync.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSyncActionPerformed(evt);
            }
        });
        jPanel5.add(btnSync);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.gridwidth = 2;
        add(jPanel5, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

private void btnSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetActionPerformed
    try {
            jp.setPlayer(player);//GEN-LAST:event_btnSetActionPerformed
        } catch (Exception ex) {
            Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
private void btnPowerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPowerActionPerformed
    try {
        if (getPlayer().isPowered()) {
            getPlayer().powerOff();
        } else {
            getPlayer().powerOn();
        }
        setLabels();
    } catch (SlimConnectionException ex) {
        Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_btnPowerActionPerformed

private void volumeSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_volumeSliderStateChanged
    setVolume(volumeSlider.getValue());
}//GEN-LAST:event_volumeSliderStateChanged

private void btnVolumeStart2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVolumeStart2ActionPerformed
    setVolume(volumeSlider.getValue() - 5);
}//GEN-LAST:event_btnVolumeStart2ActionPerformed

private void btnVolumeEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVolumeEndActionPerformed
    setVolume(volumeSlider.getValue() + 5);
}//GEN-LAST:event_btnVolumeEndActionPerformed

private void btnSleepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSleepActionPerformed
    int secs = 0;

    int hours = cbHour.getSelectedIndex();
    if (hours > 0) {
        secs += hours * 60 * 60;
    }

    int minutes = cbMinute.getSelectedIndex();
    if (minutes > 0) {
        secs += minutes * 60;
    }

    if (secs > 0) {
        try {
            getPlayer().setSleepTime(secs);
            setSleepTime(secs);
            setSleepComponentAbility(false);
            sleepTimer.start();
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        JOptionPane.showMessageDialog(this, "Must select a sleep time.", "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_btnSleepActionPerformed

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    try {
        getPlayer().setSleepTime(0);
        setSleepComponentAbility(true);
    } catch (SlimConnectionException ex) {
        Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_btnCancelActionPerformed

private void btnSyncActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSyncActionPerformed
    JDialog dialog = new PlayerSyncDialog(Utils.getParentComponent(this), true, player);
    dialog.setPreferredSize(new Dimension(300, 200));
    dialog.setMinimumSize(new Dimension(300, 200));
    Utils.centerInParent(dialog, Utils.getParentComponent(this));
    dialog.setVisible(true);
    checkSync();
}//GEN-LAST:event_btnSyncActionPerformed

private void btnUnsyncActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUnsyncActionPerformed
    try {
        getPlayer().unsyncPlayer();
        checkSync();
    } catch (SlimConnectionException ex) {
        Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_btnUnsyncActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnPower;
    private javax.swing.JButton btnSet;
    private javax.swing.JButton btnSleep;
    private javax.swing.JButton btnSync;
    private javax.swing.JButton btnUnsync;
    private javax.swing.JButton btnVolumeEnd;
    private javax.swing.JButton btnVolumeStart2;
    private javax.swing.JComboBox cbHour;
    private javax.swing.JComboBox cbMinute;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JLabel lblColon;
    private javax.swing.JLabel lblDisplayType;
    private javax.swing.JLabel lblIp;
    private javax.swing.JLabel lblMac;
    private javax.swing.JLabel lblModel;
    private javax.swing.JLabel lblPlayerName;
    private javax.swing.JLabel lblPort;
    private javax.swing.JLabel lblPower;
    private javax.swing.JLabel lblSignalStrength;
    private javax.swing.JLabel lblSleep;
    private javax.swing.JLabel lblSync;
    private javax.swing.JSlider volumeSlider;
    // End of variables declaration//GEN-END:variables

    private void setVolume(int i) {
        volumeSlider.setToolTipText("Volume:" + i);
        try {
            getPlayer().setVolume(i);
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
        }
        volumeSlider.setValue(i);
    }

    private void setLabels() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    lblPlayerName.setText(getPlayer().getName());
                    lblPower.setText(player.isPowered() ? "Yes" : "No");
                    lblIp.setText(player.getIp());
                    lblMac.setText(player.getId());
                    lblDisplayType.setText(player.getDisplayType());
                    lblModel.setText(player.getModel());
                    lblPort.setText(Integer.toString(player.getPort()));
                    lblSignalStrength.setText(Integer.toString(player.getSignalStrength()));
                } catch (SlimConnectionException ex) {
                    Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }).start();

    }

    public void hideLabels() {
        jLabel2.setVisible(false);
        jLabel3.setVisible(false);
        jLabel4.setVisible(false);
        jLabel5.setVisible(false);
        jLabel6.setVisible(false);
        jLabel7.setVisible(false);
        jLabel8.setVisible(false);
        btnPower.setVisible(false);
        lblDisplayType.setVisible(false);
        lblIp.setVisible(false);
        lblMac.setVisible(false);
        lblModel.setVisible(false);
        lblPort.setVisible(false);
        lblPower.setVisible(false);
        lblSignalStrength.setVisible(false);
        setBorder(javax.swing.BorderFactory.createTitledBorder("Players"));
    }

    @Override
    public void sleepTimeChanged(SleepChangeEvent event) {
        if (event.getPlayer().equals(getPlayer())) {

            switch (event.getId()) {
                case SleepChangeEvent.SLEEP_STARTED:
                    if (sleepThread == null || sleepThread.isStopped()) {
                        sleepThread = new SleepThread();
                        new Thread(sleepThread).start();
                    }
                    break;

                case SleepChangeEvent.SLEEP_STOPPED:
                    sleepThread.stop();
                    break;
            }
        }
    }

    /**
     * @return the player
     */
    public SlimPlayer getPlayer() {
        return player;
    }

    /**
     * @param player the player to set
     */
    private void setPlayer(SlimPlayer player) {
        this.player = player;
    }

    /**
     * @return the sleepTime
     */
    public long getSleepTime() {
        return sleepTime;
    }

    /**
     * @param sleepTime the sleepTime to set
     */
    public void setSleepTime(long sleepTime) {
        this.sleepTime = sleepTime;
    }

    private void setSleepComponentAbility(boolean b) {
        cbHour.setEnabled(b);
        cbMinute.setEnabled(b);
        btnSleep.setEnabled(b);
        btnCancel.setVisible(!b);
        if (b) {
            lblSleep.setText(MSG_NOT_SLEEPING);
        }
    }

    public void checkSync() {
        try {
            if (getPlayer().isSynced()) {
                btnSync.setVisible(false);
                btnUnsync.setVisible(true);

                String text = "Synched with";
                for (SlimPlayer p : getPlayer().getSyncedPlayerIds()) {
                    text = text + " " + p.getName();
                }
                lblSync.setText(text);
            } else {
                btnSync.setVisible(true);
                btnUnsync.setVisible(false);
                lblSync.setText("");
            }
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private class SleepThread implements Runnable {

        private boolean stopped;

        @Override
        public void run() {
            while (!stopped) {
                try {
                    setSleepTime(getPlayer().getSleepTime());
                } catch (SlimConnectionException ex) {
                    Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (btnSleep.isEnabled()) {
                    setSleepComponentAbility(false);
                }


                if (!sleepTimer.isRunning()) {
                    sleepTimer.start();
                }

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(PlayerManagerDetailPanel.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            sleepTimer.stop();
            setSleepComponentAbility(true);
        }

        public void stop() {
            stopped = true;
        }

        public boolean isStopped() {
            return !stopped;
        }
    }
}
